'''
Author: duliang thinktanker@163.com
Date: 2025-01-24 17:22:00
LastEditors: duliang thinktanker@163.com
LastEditTime: 2025-02-01 11:05:23
FilePath: \control-net\netping.py
Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
'''
import subprocess
import platform
import time
import json


def ping(host):
    # Ping参数根据操作系统可能有所不同
    print("\033 --------测试网络是否畅通----------\033[0m")
    param = '-n' if platform.system().lower() == 'windows' else '-c'
    command = ['ping', param, '5', host]
    result = subprocess.call(command) == 0
    print(f"\033 --------{result}----------\033[0m")
    return result


#
# print("Network is active." if ping('google.com') else "Network is down.")
def loop_ping():
    print("循环测速网络是否畅通")
    while 1:
        if not (ping('baidu.com')):
            print("Network is down.")
            with open(r"./data/config.json", "r", encoding="utf-8") as f:
                j = json.load(f)
                # print(j["interface"])
                subprocess.Popen(
                    f'netsh interface set interface "{j["interface"]}" admin=enable'
                )
        time.sleep(600)


if __name__ == '__main__':
    # with open(r"./data/config.json", "r", encoding="utf-8") as f:
    #     j = json.load(f)
    #     print(j["interface"])
    ping("google.com")
